class Checkrecord < ActiveRecord::Base
  belongs_to  :maintenance
  belongs_to  :worker
  belongs_to  :train_task

  def self.find_by_train_task train_task_id
    find_by_sql "select w.name worker_name,t.name team_name,tr.sn train_name,tt.stop_track track,cr.check_in_time,cr.check_out_time,cr.dynamic_check_in_time,cr.dynamic_check_out_time from train_tasks tt
                left join maintenances m on tt.id = m.train_task_id
                left join trains tr on tt.train_id = tr.id
                left join teams t on m.team_id = t.id
                left join workers w on t.id = w.team_id
                left join checkrecords cr on cr.train_task_id = tt.id AND cr.worker_id = w.id
                where tt.id = #{train_task_id} AND (w.is_manager = '1' OR cr.check_in_time IS NOT NULL OR cr.dynamic_check_in_time IS NOT NULL)
                order by t.name,cr.check_in_time"
  end
end
